package com.lihepeng.leecode.array;

import org.junit.Test;

/**
 *给你一个二维整数数组 matrix， 返回 matrix 的 转置矩阵 。
 *
 * 矩阵的 转置 是指将矩阵的主对角线翻转，交换矩阵的行索引与列索引。
 */
public class Solution867 {
    public int[][] transpose(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int [][]tranMatrix = new int [n][m];
        for (int i=0;i<m;i++){
            for(int j =0;j<n;j++){
                tranMatrix[j][i] = matrix[i][j];
            }
        }
        return tranMatrix;

    }
    @Test
    public void runTest(){
        int [][] matrix = {{1,2,3},{4,5,6}};
        int[][] transpose = transpose(matrix);
        this.printMatrix(transpose);
    }
    @Test
    public void runTest01(){
        int [][] matrix = {{1,2,3},{4,5,6}};
        this.printMatrix(matrix);
    }
    private void printMatrix(int [][]matrix){
        int m = matrix.length;
        int n = matrix[0].length;
        for (int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                System.out.print(matrix[i][j]);
                System.out.print(" ");
            }
            System.out.println( );
        }
    }
}
